/**
 * This file is part of the DbUnit module for NetBeans IDE
 * Copyright (C) 2009 Pavol Pitoňák
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

package cz.muni.fi.dbunit;

import java.awt.Dialog;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectInformation;
import org.netbeans.spi.project.ui.LogicalViewProvider;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;

/**
 *
 * @author Pavol Pitoňák <pavol@pitonak.com>
 */
public final class SimpleTestVisualPanel extends JPanel {

    public static final String CLASS_TO_TEST = "class_to_test";
    public static final String PACKAGE = "package";
    public static final String PUBLIC_METHODS = "public_methods";
    public static final String PROTECTED_METHODS = "protected_methods";
    public static final String PACKAGE_PRIVATE = "package_private";
    public static final String TEST_INITIALIZER = "test_initializer";
    public static final String TEST_FINALIZER = "test_finalizer";
    public static final String DEFAULT_METHOD_BODIES = "default_method_bodies";
    public static final String GENERATE_JAVADOC = "generate_javadoc";
    public static final String GENERATE_HINTS = "generate_hints";
    private Node node;
    private Node[] srcPackages;
    private Project project;
    private final List<ChangeListener> listeners = new ArrayList<ChangeListener>();
    private List<String> locations;
    private boolean valideClass;
    private boolean maven = false;

    private class LocationComboBoxModel extends AbstractListModel implements ComboBoxModel {

        private List<String> items;
        private Object selectedItem;

        // TODO implement it in a better way
        public LocationComboBoxModel(List<String> items) {
            this.items = items;
            selectedItem = items.get(0);
        }

        public int getSize() {
            return items.size();
        }

        public Object getElementAt(int index) {
            if (index >= getSize() || index < 0) {
                throw new ArrayIndexOutOfBoundsException("index");
            }
            return items.get(index);
        }

        public void setSelectedItem(Object anItem) {
            selectedItem = anItem;
        }

        public Object getSelectedItem() {
            return selectedItem;
        }
    }

    /** Creates new form SimpleTestVisualPanel */
    public SimpleTestVisualPanel(Project p, String className) {
        project = p;

        LogicalViewProvider lvp = p.getLookup().lookup(LogicalViewProvider.class);

        node = lvp.createLogicalView().getChildren().findChild("${src.dir}");
        if (node == null) {
            node = lvp.createLogicalView().getChildren().findChild("1SourceRoot");
        }
        srcPackages = node.getChildren().getNodes(true);

        locations = new ArrayList<String>();
        Node tmpNode = lvp.createLogicalView().getChildren().findChild("${test.src.dir}");
        if (tmpNode == null) {
            tmpNode = lvp.createLogicalView().getChildren().findChild("2TestSourceRoot");
            maven = true;
        }
        // TODO implement it in a better way
        // this works only with one directory
        locations.add(tmpNode.getDisplayName());

        initComponents();
        ProjectInformation info = p.getLookup().lookup(ProjectInformation.class);
        projectTextField.setText(info.getDisplayName());
        valideClass = false;
        classToTestTextField.setText("");

        if (className == null) {
            browseButton.setEnabled(false);
            classToTestTextField.setText(
                    NbBundle.getMessage(SimpleTestVisualPanel.class,
                    "SimpleTestVisualPanel.classToTestTextField.ifMoreClasses"));
            createdTestClassTextField.setText("");
            createdFileTextField.setText("");
            valideClass = true;
            fireChange();
        } else if (!"".equals(className)) {
            classToTestTextField.setText(className);
        }
    }

    @Override
    public String getName() {
        return NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.name");
    }

    public String getClassName() {
        String tmp = classToTestTextField.getText();
        return tmp;
    }

    public String getPackage() {
        int idx = classToTestTextField.getText().lastIndexOf("."); // index of last dot
        return classToTestTextField.getText().substring(0, idx);
    }

    public String getFileName() {
        return project.getProjectDirectory() + "/test/" + buildPathFromClassName() + ".java";
    }

    private String buildPathFromClassName() {
        return getClassName().replace(".", "/");
    }

    public boolean isPublicMethods() {
        return publicCheckBox.isSelected();
    }

    public boolean isProtectedMethods() {
        return protectedCheckBox.isSelected();
    }

    public boolean isPackagePrivateMethods() {
        return packagePrivateCheckBox.isSelected();
    }

    public boolean isTestInitializer() {
        return testInitializerCheckBox.isSelected();
    }

    public boolean isTestFinalizer() {
        return testFinalizerCheckBox.isSelected();
    }

    public boolean isDefaultMethodBodies() {
        return defaultMethodBodiesCheckBox.isSelected();
    }

    public boolean isJavadocComments() {
        return javadocCommentsCheckBox.isSelected();
    }

    public boolean isSourceCodeHints() {
        return sourceCodeHintsCheckBox.isSelected();
    }

    public boolean isValideClass() {
        return valideClass;
    }

    public void setJavadocComments(boolean arg) {
        javadocCommentsCheckBox.setSelected(arg);
    }

    public void setMethodBodies(boolean arg) {
        defaultMethodBodiesCheckBox.setSelected(arg);
    }

    public void setPackagePrivateMethods(boolean arg) {
        packagePrivateCheckBox.setSelected(arg);
    }

    public void setProtectedMethods(boolean arg) {
        protectedCheckBox.setSelected(arg);
    }

    public void setPublicMethods(boolean arg) {
        publicCheckBox.setSelected(arg);
    }

    public void setSourceCodeHints(boolean arg) {
        sourceCodeHintsCheckBox.setSelected(arg);
    }

    public void setTestFinalizer(boolean arg) {
        testFinalizerCheckBox.setSelected(arg);
    }

    public void setTestInitializer(boolean arg) {
        testInitializerCheckBox.setSelected(arg);
    }

    public JTextField getClassToTestTextField() {
        return classToTestTextField;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        classToTestLabel = new javax.swing.JLabel();
        classToTestTextField = new javax.swing.JTextField();
        browseButton = new javax.swing.JButton();
        createdTestClassLabel = new javax.swing.JLabel();
        createdTestClassTextField = new javax.swing.JTextField();
        projectLabel = new javax.swing.JLabel();
        projectTextField = new javax.swing.JTextField();
        locationLabel = new javax.swing.JLabel();
        locationComboBox = new javax.swing.JComboBox();
        createdFileLabel = new javax.swing.JLabel();
        createdFileTextField = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        methodAccessLevelsLabel = new javax.swing.JLabel();
        publicCheckBox = new javax.swing.JCheckBox();
        protectedCheckBox = new javax.swing.JCheckBox();
        packagePrivateCheckBox = new javax.swing.JCheckBox();
        generatedCodeLabel = new javax.swing.JLabel();
        testInitializerCheckBox = new javax.swing.JCheckBox();
        testFinalizerCheckBox = new javax.swing.JCheckBox();
        defaultMethodBodiesCheckBox = new javax.swing.JCheckBox();
        generatedCommentsLabel = new javax.swing.JLabel();
        javadocCommentsCheckBox = new javax.swing.JCheckBox();
        sourceCodeHintsCheckBox = new javax.swing.JCheckBox();

        org.openide.awt.Mnemonics.setLocalizedText(classToTestLabel, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.classToTestLabel.text")); // NOI18N

        classToTestTextField.setEditable(false);
        classToTestTextField.setText(org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.classToTestTextField.text")); // NOI18N
        classToTestTextField.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                classToTestTextFieldCaretUpdate(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.browseButton.text")); // NOI18N
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(createdTestClassLabel, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.createdTestClassLabel.text")); // NOI18N

        createdTestClassTextField.setEditable(false);
        createdTestClassTextField.setText(org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.createdTestClassTextField.text")); // NOI18N
        createdTestClassTextField.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                createdTestClassTextFieldCaretUpdate(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(projectLabel, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.projectLabel.text")); // NOI18N

        projectTextField.setEditable(false);
        projectTextField.setText(org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.projectTextField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(locationLabel, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.locationLabel.text")); // NOI18N

        locationComboBox.setModel(new LocationComboBoxModel(locations));

        org.openide.awt.Mnemonics.setLocalizedText(createdFileLabel, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.createdFileLabel.text")); // NOI18N

        createdFileTextField.setEditable(false);
        createdFileTextField.setText(org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.createdFileTextField.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(methodAccessLevelsLabel, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.methodAccessLevelsLabel.text")); // NOI18N

        publicCheckBox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(publicCheckBox, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.publicCheckBox.text")); // NOI18N

        protectedCheckBox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(protectedCheckBox, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.protectedCheckBox.text")); // NOI18N

        packagePrivateCheckBox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(packagePrivateCheckBox, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.packagePrivateCheckBox.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(generatedCodeLabel, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.generatedCodeLabel.text")); // NOI18N

        testInitializerCheckBox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(testInitializerCheckBox, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.testInitializerCheckBox.text")); // NOI18N

        testFinalizerCheckBox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(testFinalizerCheckBox, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.testFinalizerCheckBox.text")); // NOI18N

        defaultMethodBodiesCheckBox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(defaultMethodBodiesCheckBox, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.defaultMethodBodiesCheckBox.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(generatedCommentsLabel, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.generatedCommentsLabel.text")); // NOI18N

        javadocCommentsCheckBox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(javadocCommentsCheckBox, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.javadocCommentsCheckBox.text")); // NOI18N

        sourceCodeHintsCheckBox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(sourceCodeHintsCheckBox, org.openide.util.NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.sourceCodeHintsCheckBox.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 1296, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(methodAccessLevelsLabel)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(packagePrivateCheckBox)
                                    .addComponent(protectedCheckBox)
                                    .addComponent(publicCheckBox))))
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(generatedCodeLabel)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(testFinalizerCheckBox)
                                    .addComponent(testInitializerCheckBox)
                                    .addComponent(defaultMethodBodiesCheckBox))))
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(generatedCommentsLabel)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(sourceCodeHintsCheckBox)
                                    .addComponent(javadocCommentsCheckBox))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 740, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(createdTestClassLabel)
                            .addComponent(classToTestLabel)
                            .addComponent(projectLabel)
                            .addComponent(locationLabel)
                            .addComponent(createdFileLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(createdFileTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1164, Short.MAX_VALUE)
                            .addComponent(locationComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, 1164, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(createdTestClassTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 1069, Short.MAX_VALUE)
                                    .addComponent(classToTestTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 1069, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(browseButton))
                            .addComponent(projectTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1164, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(classToTestLabel)
                    .addComponent(classToTestTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createdTestClassLabel)
                    .addComponent(createdTestClassTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectLabel)
                    .addComponent(projectTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(locationLabel)
                    .addComponent(locationComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createdFileLabel)
                    .addComponent(createdFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(methodAccessLevelsLabel)
                    .addComponent(generatedCodeLabel)
                    .addComponent(generatedCommentsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(publicCheckBox)
                    .addComponent(testInitializerCheckBox)
                    .addComponent(javadocCommentsCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(protectedCheckBox)
                    .addComponent(testFinalizerCheckBox)
                    .addComponent(sourceCodeHintsCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(packagePrivateCheckBox)
                    .addComponent(defaultMethodBodiesCheckBox))
                .addGap(65, 65, 65))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
        JavaClassChooser myChooser = new JavaClassChooser(node);
        final DialogDescriptor dd = new DialogDescriptor(myChooser,
                NbBundle.getMessage(SimpleTestVisualPanel.class, "SimpleTestVisualPanel.windowTitle"));

        dd.setValid(false);
        myChooser.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(JavaClassChooser.IS_VALID)) {
                    Object newvalue = evt.getNewValue();
                    if ((newvalue != null) && (newvalue instanceof Boolean)) {
                        dd.setValid(((Boolean) newvalue).booleanValue());
                    }
                }
            }
        });

        Dialog dlg = DialogDisplayer.getDefault().createDialog(dd);
        dlg.getAccessibleContext().setAccessibleDescription(dlg.getTitle());
        dlg.setVisible(true);

        if (dd.getValue() == DialogDescriptor.OK_OPTION) {
            classToTestTextField.setText(myChooser.getClassName());
            fireChange();
        }
    }//GEN-LAST:event_browseButtonActionPerformed

    private void classToTestTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_classToTestTextFieldCaretUpdate
        if (classToTestTextField.getText() == null || classToTestTextField.getText().equals("")) {
            createdTestClassTextField.setText("");
            valideClass = false;
            fireChange();
            return;
        } else {
            createdTestClassTextField.setText(classToTestTextField.getText() + "Test");
        }

        String tmpString = null;

        for (int i = 0; i < srcPackages.length; i++) {
            for (int j = 0; j < srcPackages[i].getChildren().getNodesCount(); j++) {
                tmpString = srcPackages[i].getDisplayName() + "." +
                        srcPackages[i].getChildren().getNodeAt(j).getDisplayName().replace(".java", "");
                if (classToTestTextField.getText().equals(tmpString)) {
                    valideClass = true;
                    fireChange();
                    return;
                }
            }
        }

        valideClass = false;
        fireChange();
    }//GEN-LAST:event_classToTestTextFieldCaretUpdate

    private void createdTestClassTextFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_createdTestClassTextFieldCaretUpdate
        if (createdTestClassTextField.getText() == null || createdTestClassTextField.getText().equals("")) {
            createdFileTextField.setText("");
            return;
        }

        StringBuilder fileName = new StringBuilder("/");
        fileName.append(project.getProjectDirectory().getPath());
        fileName.append("/test/");
        if (maven) {
            fileName.append("java/");
        }
        fileName.append(createdTestClassTextField.getText().replace(".", "/"));
        fileName.append(".java");

        createdFileTextField.setText(fileName.toString());
    }//GEN-LAST:event_createdTestClassTextFieldCaretUpdate

    private void fireChange() {
        ChangeEvent e = new ChangeEvent(this);
        Iterator<ChangeListener> it = listeners.iterator();
        while (it.hasNext()) {
            it.next().stateChanged(e);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseButton;
    private javax.swing.JLabel classToTestLabel;
    private javax.swing.JTextField classToTestTextField;
    private javax.swing.JLabel createdFileLabel;
    private javax.swing.JTextField createdFileTextField;
    private javax.swing.JLabel createdTestClassLabel;
    private javax.swing.JTextField createdTestClassTextField;
    private javax.swing.JCheckBox defaultMethodBodiesCheckBox;
    private javax.swing.JLabel generatedCodeLabel;
    private javax.swing.JLabel generatedCommentsLabel;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JCheckBox javadocCommentsCheckBox;
    private javax.swing.JComboBox locationComboBox;
    private javax.swing.JLabel locationLabel;
    private javax.swing.JLabel methodAccessLevelsLabel;
    private javax.swing.JCheckBox packagePrivateCheckBox;
    private javax.swing.JLabel projectLabel;
    private javax.swing.JTextField projectTextField;
    private javax.swing.JCheckBox protectedCheckBox;
    private javax.swing.JCheckBox publicCheckBox;
    private javax.swing.JCheckBox sourceCodeHintsCheckBox;
    private javax.swing.JCheckBox testFinalizerCheckBox;
    private javax.swing.JCheckBox testInitializerCheckBox;
    // End of variables declaration//GEN-END:variables
}

